package com.xiangxiangjd.serviceImpl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiangxiangjd.bean.SysDict;
import com.xiangxiangjd.bean.result.ResultPage;
import com.xiangxiangjd.controller.helper.$;
import com.xiangxiangjd.controller.model.ApiResultModel;
import com.xiangxiangjd.mapper.SysDictMapper;
import com.xiangxiangjd.service.SysDictService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 肖守凯
 * @since 2020-10-23
 */
@Service
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements SysDictService {
    /** 添加SysDict
     * @param sysDict  前端传进的参数
     * @return 返回统一信息
     */
    @Override
    public ApiResultModel addSysDict(SysDict sysDict  ) {
           if(this.save(sysDict )){
               return $.success();
           }else{
                return $.error("添加失败");
           }
    }
    /** 编辑跳转页面的回显数据SysDict
     * @param id  前端传进的参数
     * @return 返回实体类
     */
    @Override
    public SysDict selectSysDictByIdSysDict(String id ){
            return this.getById(id);
    }


    /** 编辑SysDict
     * @param sysDict  前端传进的参数
     * @return 返回统一信息
     */
    @Override
    public ApiResultModel editSysDict(SysDict sysDict  ) {
           if(this.updateById(sysDict )){
               return $.success();
           }else{
                return $.error("编辑失败");
           }
    }
    /** 删除一条SysDict
     * @param id  前端传进的ID
     * @return 返回统一信息
     */
    @Override
    public   ApiResultModel delSysDict( String id  ){
           if(this.removeById(id)){
               return $.success();
           }else{
                return $.error("删除失败");
           }
    }

   /** 分页查询SysDict
     * @param page  页码
     * @param limit  查几条
     * @param sysDict  前端传进的参数
     * @return 返回统一分页信息
     */
    @Override
    public ResultPage findSysDictByPage(Integer page, Integer limit, SysDict sysDict){
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("page", (page - 1) * limit);
        paramMap.put("limit", limit);
        paramMap.put("sysDict", sysDict);
        List<SysDict> list = baseMapper.selectMyPage(paramMap);
        Integer count = baseMapper.countMyPage(paramMap);
        ResultPage<SysDict> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }

    @Override
    public String selectByDatasourceKey(String type, String key) {
        SysDict dict=new SysDict();
        dict.setK(key);
        dict.setType(type);
        String val = baseMapper.selectByDatasourceCode(dict).getVal();
        System.out.println("val:"+val);
        return val;
    }

}
